// pages/user/company/compamyData/companyData.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        type: null,
        user: null,
        class: null,
        dataList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        searchKey: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            type: options.type,
            user: options.user,
            class: options.class
        })
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getDataList()
    },

    getUserInfo: function () {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
                that.getDataList()
            }
        }).catch(function (e) { })
    },

    getDataList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let dataList = this.data.dataList
        let data
        if (this.data.searchKey == null) {
            if (this.data.type == 'manager') {
                if (this.data.class == 'user') {
                    data = {
                        client: 2,
                        user_type: this.data.user,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
                else {
                    data = {
                        client: 2,
                        store_type: this.data.user,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            } else if (this.data.type == 'filiale') {
                if (this.data.class == 'user') {
                    data = {
                        client: 2,
                        user_type: this.data.user,
                        filiale_id: this.data.userInfo.filiale_id,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
                else {
                    data = {
                        client: 2,
                        store_type: this.data.user,
                        filiale_id: this.data.userInfo.filiale_id,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            } else if (this.data.type == 'shop') {
                if (this.data.class == 'user') {
                    data = {
                        client: 2,
                        user_type: this.data.user,
                        store_id: this.data.userInfo.store_id,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
                else {
                    data = {
                        client: 2,
                        store_type: this.data.user,
                        store_id: this.data.userInfo.store_id,
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            }
        } else {
            data = {
                user_id: this.data.searchKey
            }
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('User/user_list', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    dataList: dataList,
                    isAll: true
                })
            } else {
                dataList = dataList.concat(res)
                that.setData({
                    dataList: dataList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function (e) { })
    },

    phoneCall: function (e) {
        if (e.currentTarget.dataset.phone == '0') {
            wx.showToast({
                title: '该用户未绑定手机号码',
                icon: 'none',
                duration: 1000
            })
            return
        }
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        })
    },

    searchInput: function (e) {
        this.setData({
            searchKey: e.detail.value
        })
    },

    searchClick: function () {
        const that = this
        if (!Number(this.data.searchKey) && Number(this.data.searchKey) != 0) {
            wx.showModal({
                title: '提示',
                content: '请输入用户ID进行搜索',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            searchKey: null
                        })
                    }
                }
            })
            return
        }
        this.setData({
            dataList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getDataList()
    }
})